package main

// <---------------------------------- http部分 -------------------------------------------->

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

/*
	功能：POST请求，发送json
	参数：1. url   http 地址
		2. map2  要转发的map(已经删减好的)
*/
func HttpPostJson(url string, map2 map[string]string) {

	bf := bytes.NewBuffer([]byte{}) // 我们转换程的json
	jsonEncoder := json.NewEncoder(bf)
	jsonEncoder.SetEscapeHTML(false)
	jsonEncoder.Encode(map2)

	// var jsonStr = []byte(post)
	// fmt.Println("jsonStr", jsonStr)
	// fmt.Println("new_str", bytes.NewBuffer(jsonStr))
	fmt.Println("json is : ", bf)

	req, err := http.NewRequest("POST", url, bf)
	// req.Header.Set("X-Custom-Header", "myvalue")
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		//panic(err)
		fmt.Println("post error is : ", err)
	} else {
		defer resp.Body.Close()
		fmt.Println("status", resp.Status)
		fmt.Println("response:", resp.Header)
		body, _ := ioutil.ReadAll(resp.Body)
		fmt.Println("response Body 返回来的回复:", string(body))
	}

}

/*
	功能：调用http转发函数
	函数：1. http_url  http地址
		2. http_verb  转发的形式
		3. http_mime  转发的格式
		4. map_user   要转发的map(已经删减好的)
*/
func HttpWork(http_url string, http_verb string, http_mime string, map_user_del map[string]string) {

	switch http_mime {

	case "json":
		HttpPostJson(http_url, map_user_del)
	default:

	}
}
